﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Linq;
using System.Xml.Serialization;

namespace winXml
{
    /// <summary>
    /// XML操作帮助类
    /// </summary>
    public class XmlHelper
    {

        public static XDocument doc = null;
        /// <summary>
        /// 根元素
        /// </summary>
        public static string RootElement
        {
            get;
            set;
        }

        /// <summary>
        /// XML路径
        /// </summary>
        public static string XmlPath
        {
            get;
            set;
        }
        public XmlHelper(string xmlPath, string rootName)
        {
            if (!File.Exists(xmlPath))
            {
                throw new FileNotFoundException("未能找到配置文件" + xmlPath);
            }
            RootElement = rootName;
            XmlPath = xmlPath;
            doc = XDocument.Load(xmlPath);
        }
        #region 获得/设置自定义xml的值
        /// <summary>
        /// 根据元素名称获取值
        /// </summary>
        /// <param name="xname"></param>
        /// <returns></returns>
        public static string GetValue(string xname)
        {
            if (!File.Exists(XmlPath))
            {
                throw new FileNotFoundException("未能找到配置文件" + XmlPath);
            }
            doc = XDocument.Load(XmlPath);
            XElement xml = doc.Elements(RootElement).ElementAt(0);
            XAttribute xAttribute = xml.Element(xname).Attribute("Value");
            if (xAttribute != null)
                return xAttribute.Value;
            return null;
        }
        /// <summary>
        /// 根据元素名称获取值
        /// </summary>
        /// <param name="xname1">元素1</param>
        /// <param name="xname2">元素1</param>
        /// <returns></returns>
        public static Dictionary<string, string> GetValue(string xname1, string xname2)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            XDocument doc = XDocument.Load(XmlPath);
            XElement xml = doc.Elements(RootElement).Elements(xname1).ElementAt(0).Element(xname2);
            foreach (XAttribute xAttribute in xml.Attributes())
            {
                dic.Add(xAttribute.Name.ToString(), xAttribute.Value.ToString());
            }
            return dic;
        }
        /// <summary>
        /// 根据元素名称设置值
        /// </summary>
        /// <param name="xname"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool SetValue1(string xname, string value)
        {
            try
            {
                XDocument doc = XDocument.Load(XmlPath);
                XElement xml = doc.Elements(RootElement).ElementAt(0);
                XAttribute xAttribute = xml.Element(xname).Attribute("Value");
                if (xAttribute != null)
                    xAttribute.SetValue(value);
                doc.Save(XmlPath);
            }
            catch (Exception e)
            {
                throw e;
            }
            return true;
        }
        /// <summary>
        /// 根据元素名称设置值
        /// </summary>
        /// <param name="xname1"></param>
        /// <param name="xname2"></param>
        /// <param name="value"></param>
        public static void SetValue(string xname1, string xname2, string value)
        {
            XDocument doc = XDocument.Load(XmlPath);
            XElement xml = doc.Elements(RootElement).Elements(xname1).ElementAt(0);
            XAttribute xAttribute = xml.Element(xname2).Attribute("Value");
            if (xAttribute != null)
                xAttribute.SetValue(value);
            doc.Save(XmlPath);
        }
        /// <summary>
        /// 根据元素名称设置值
        /// </summary>
        /// <param name="xname"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool SetValue(string xname, string value)
        {
            try
            {
                XElement xml = doc.Elements(RootElement).ElementAt(0);
                XAttribute xAttribute = xml.Element(xname).Attribute("Value");
                if (xAttribute != null)
                    xAttribute.SetValue(value);
                doc.Save(XmlPath);
            }
            catch (Exception e)
            {
                throw e;
            }
            return true;
        }

        /// <summary>
        /// 根据元素名称设置值
        /// </summary>
        /// <param name="xname1"></param>
        /// <param name="xname2"></param>
        /// <param name="attribute"></param>
        /// <param name="value"></param>
        public static void SetValue(string xname1, string xname2, string attribute, string value)
        {
            XDocument doc = XDocument.Load(XmlPath);
            XElement xml = doc.Elements(RootElement).Elements(xname1).ElementAt(0);
            XAttribute xAttribute = xml.Element(xname2).Attribute(attribute);
            if (xAttribute != null)
                xAttribute.SetValue(value);
            doc.Save(XmlPath);
        }
        #endregion

        #region 反序列化  
        /// <summary>  
        /// 反序列化  
        /// </summary>  
        /// <param name="type">类型</param>  
        /// <param name="xml">XML字符串</param>  
        /// <returns></returns>  
        public static object Deserialize(Type type, string xml)
        {
            using (StringReader sr = new StringReader(xml))
            {
                XmlSerializer xmldes = new XmlSerializer(type);
                return xmldes.Deserialize(sr);
            }
        }
        /// <summary>  
        /// 反序列化  
        /// </summary>  
        /// <param name="type"></param>  
        /// <param name="xml"></param>  
        /// <returns></returns>  
        public static object Deserialize(Type type, Stream stream)
        {
            XmlSerializer xmldes = new XmlSerializer(type);
            return xmldes.Deserialize(stream);
        }
        #endregion

        #region 序列化  
        /// <summary>  
        /// 序列化  
        /// </summary>  
        /// <param name="type">类型</param>  
        /// <param name="obj">对象</param>  
        /// <returns></returns>  
        public static string Serializer(Type type, object obj)
        {
            MemoryStream Stream = new MemoryStream();
            XmlSerializer xml = new XmlSerializer(type);
            //序列化对象  
            xml.Serialize(Stream, obj);
            Stream.Position = 0;
            StreamReader sr = new StreamReader(Stream);
            string str = sr.ReadToEnd();

            sr.Dispose();
            Stream.Dispose();

            return str;
        }

        #endregion
    }
}
